package org.lds.ers.client.widgets;

import java.util.List;

import org.lds.ers.client.StakeService;
import org.lds.ers.client.StakeServiceAsync;
import org.lds.ers.client.model.StakeERSCInfo;
import org.lds.ers.client.model.StakeInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MaintainStakeERS extends VerticalPanel {
	private static MaintainStakeERS singleton;
	public static MaintainStakeERS getInstance()
	{
		if (singleton==null)
			singleton = new MaintainStakeERS();
		
		return singleton;
	}

    final private Label lblBanner;
	final private VerticalPanel pnl1;
	final private ListBox cboStake; 
	final private VerticalPanel pnlERS;
	final private TextBox txtUserID;
	final private TextBox txtRealName;

	private StakeServiceAsync stakeService = GWT.create(StakeService.class);
	
	private MaintainStakeERS()
	{
		this.lblBanner = new Label("[[管理支聯會 ERSC 專員]]");
		this.add(this.lblBanner);
		
		//this.self = this;
		this.pnl1 = new VerticalPanel();
		this.add(pnl1);
		
		this.pnl1.add(new Label("選擇支聯會："));
		//Add ComboBox
		cboStake = new ListBox(false);
		cboStake.addChangeHandler(new ChangeHandler() {

			public void onChange(ChangeEvent event) {
				// TODO Auto-generated method stub
				refreshERS();
			}
			
		});
		this.pnl1.add(cboStake);
		//Add panel for ERS list
		pnlERS = new VerticalPanel();
		this.pnl1.add(pnlERS);
		
		//Add a panel for insert new ERS records.
		final DecoratorPanel pnlAddNew = new DecoratorPanel();
		pnlAddNew.setVisible(false);
		final Button btnAdd = new Button("儲存");
		btnAdd.setEnabled(false);
		
		FlexTable editForm = new FlexTable();
		editForm.setWidget(0, 0, new Label("使用者ID :"));
		this.txtUserID = new TextBox();
		this.txtUserID.addKeyPressHandler(new KeyPressHandler(){
			public void onKeyPress(KeyPressEvent event) {
				btnAdd.setEnabled(!txtUserID.getText().equals(""));
			}
		});
		editForm.setWidget(0,1,this.txtUserID);
		
		editForm.setWidget(1, 0, new Label("使用者姓名 :"));
		this.txtRealName = new TextBox();
		editForm.setWidget(1,1,this.txtRealName);
		
		btnAdd.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				btnAdd.setEnabled(false);
				if (cboStake.getSelectedIndex() > -1)
				{
					StakeERSCInfo ersc = new StakeERSCInfo();
					ersc.setUserID(txtUserID.getText());
					ersc.setRealName(txtRealName.getText());
					ersc.setStakeID(Long.parseLong(cboStake.getValue(cboStake.getSelectedIndex())));
				
					stakeService.insertERSC(ersc, new AsyncCallback<StakeERSCInfo>() {

						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							btnAdd.setEnabled(true);
							Window.alert(caught.getMessage());
						}

						public void onSuccess(StakeERSCInfo result) {
							// TODO Auto-generated method stub
							txtUserID.setText("");
							txtRealName.setText("");
							btnAdd.setEnabled(false);
							pnlAddNew.setVisible(false);
							refreshERS();
						}
				    });
				}
			}
		});
		editForm.setWidget(1,2, btnAdd);
		final Button btnCancel = new Button("取消");
		btnCancel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				pnlAddNew.setVisible(false);
				txtUserID.setText("");
				txtRealName.setText("");
			}
		});
		editForm.setWidget(1,3,btnCancel);
		
		pnlAddNew.add(editForm);
		
		
		final Button btnShowAddNew = new Button("新增");
		btnShowAddNew.addClickHandler(new ClickHandler(){

			public void onClick(ClickEvent event) {
				pnlAddNew.setVisible(true);
			}
		});
		
		this.pnl1.add(btnShowAddNew);
		this.pnl1.add(pnlAddNew);
		
		refreshStakes();
		refreshERS();
	}
	
	public void refreshStakes()
	{
		stakeService.getAll(new AsyncCallback<List<StakeInfo>>() {

			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Window.alert(caught.getMessage());
			}

			public void onSuccess(List<StakeInfo> result) {
				// TODO Auto-generated method stub
				cboStake.clear();
				for(StakeInfo s : result) {
					cboStake.addItem(s.getStakeName() + "(" + s.getStakeNo() + ")", s.getId() + "");
				}
				if (result.size() > 0)
					cboStake.setItemSelected(0, true);
				refreshERS();
			}
	    });
	}
	public void refreshERS()
	{
	
		if (this.cboStake.getSelectedIndex() == -1)
			return ;
		Long stakeID = Long.parseLong(this.cboStake.getValue(this.cboStake.getSelectedIndex()));
		stakeService.getAllERSC(stakeID, new AsyncCallback<List<StakeERSCInfo>>() {

			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			public void onSuccess(List<StakeERSCInfo> result) {
				pnlERS.clear();
				final FlexTable flexTable =new FlexTable();
			    
			    flexTable.addStyleName("cw-FlexTable");
			    flexTable.setWidth("32em");
			    flexTable.setBorderWidth(1);
			    flexTable.setCellSpacing(0);
			    flexTable.setCellPadding(3);
			    flexTable.setText(0, 0, "ERS專員代碼：" );
				flexTable.setText(0, 1, "ERS專員姓名：" );
				flexTable.setText(0, 2, "");
			    
			    int row = 1;
				for(StakeERSCInfo s : result )
				{
					addRow(row, flexTable, s);
					row += 1;
				}
				pnlERS.add(flexTable);
				
			}
	    });	
	}
	
	private void addRow(int row, FlexTable table, StakeERSCInfo info)
	{
		final StakeERSCInfo ersc = info;
		table.setText(row, 0, ersc.getUserID() );
		table.setText(row, 1, ersc.getRealName() );
		
		final Button btnDelete = new Button("移除");
		btnDelete.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (Window.confirm("您確定要移除這位 ERSC 專員：『" + ersc.getRealName() + "(" +  ersc.getUserID()  + ")』"))
				{
					btnDelete.setEnabled(false);
					stakeService.deleteERSC(ersc.getID(), new AsyncCallback<Boolean>() {
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							btnDelete.setEnabled(true);
							Window.alert(caught.getMessage());
						}

						public void onSuccess(Boolean result) {
							
							refreshERS();
						}
				    });
				}
			}
		});
		
		table.setWidget(row, 2, btnDelete );
	}
	
}
